package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-09-23 11:50
 */
@Component
public class MyJob106 {

    public List<Integer> getAllData(){
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            integers.add(i);
        }
        return integers;
    }

    @XxlJob("testJob")
    public void test(){

        int shardIndex = XxlJobHelper.getShardIndex(); //当前服务实例下标  0 1
        int shardTotal = XxlJobHelper.getShardTotal(); //当前集群中总实例数 2

        //启动两个实例，第一个实例拿到奇数数据，第二个实例拿到偶数数据
        List<Integer> allData = getAllData();
        for (Integer allDatum : allData) {
            if(allDatum % shardTotal == shardIndex){
                //这个数据是我的，打印
                System.out.println(allDatum);
            }
        }
    }
}
